<template>
  <loginHeader>联合登录</loginHeader>
  <section class="container" v-if="isBind">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a
        @click="hasAccount = true"
        :class="{ active: hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-bind" />
        <span>已有稿定账号，请绑定手机</span>
      </a>
      <a
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-edit" />
        <span>没有稿定账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <callbackBind :unionId="unionId"></callbackBind>
    </div>
    <div class="tab-content" v-else>
      <callbackPatch :unionId="unionId"></callbackPatch>
    </div>
  </section>
  <loginFooter></loginFooter>
</template>

<script>
import { ref } from 'vue'
import loginHeader from './components/login-header.vue'
import loginFooter from './components/login-footer.vue'
import callbackBind from '@/views/login/components/callback-bind'
import callbackPatch from '@/views/login/components/callback-patch'
import QC from 'qc'
import { userQQLogin } from '@/api/user'
import { message } from '../../components/library/message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'Callback',
  components: { loginHeader, loginFooter, callbackBind, callbackPatch },
  setup() {
    const hasAccount = ref(true)

    // 假设已经绑定，默认会去做一次登录，如果登录失败证明未绑定。
    const isBind = ref(true)
    // 1. 获取QQ互联的openId也就是后台需要的unionId
    // 2. 根据QQ互联的openId去进行登录，准备一个接口
    const store = useStore()
    const router = useRouter()
    const unionId = ref(null)
    if (QC.Login.check()) {
      // 检查QQ是否登录
      QC.Login.getMe((openId) => {
        unionId.value = openId
        // 请求小兔鲜后台，做QQ登录
        userQQLogin(openId)
          .then((data) => {
            // 登录成功：data.result 用户信息
            // 1. 存储用户信息
            const { id, account, avatar, mobile, nickname, token } = data.result
            store.commit('user/setUser', {
              id,
              account,
              avatar,
              mobile,
              nickname,
              token
            })
            store.dispatch('cart/mergeCart').then(() => {
              // 2. 跳转到来源页或者首页
              router.push(store.state.user.redirectUrl)
              // 3. 成功提示
              message({ type: 'success', text: 'QQ登录成功' })
            })
          })
          .catch((e) => {
            // 登录失败：没有和小兔鲜绑定
            isBind.value = false
          })
      })
    }
    return { hasAccount, isBind, unionId }
  }
}
</script>
<style scoped lang="less">
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}

.tab {
  background-color: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 1px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @color;
      border-color: @color;
    }
  }
}
.tab-content {
  min-height: 600px;
  background-color: #fff;
}
</style>
